<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :isroot="true" />

        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template #default="{data}">
            <tree-tools :node-data="data" @delCompanyDept="getCompanyDept" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dialog :dialog-visible="dialogVisible" @close="closeDialog" />
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import treeTools from './components/tree-tools.vue'
import { reqCompanyDept } from '@/api/departments'
import AddDialog from './components/add-dialog.vue'
export default {
  components: { treeTools, AddDialog },
  data() {
    return {
      dialogVisible: true,
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getCompanyDept()
  },
  methods: {
    async getCompanyDept() {
      const { data: { depts, companyName }} = await reqCompanyDept()
      console.log(JSON.parse(JSON.stringify(depts)))
      this.departs = tranListToTreeData(depts, '')
      this.company.name = companyName
    },
    closeDialog(flag) {
      this.dialogVisible = flag
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
</style>
